package com.cake.liulanxiangzhu.service.impl;

import com.cake.liulanxiangzhu.pojo.entity.UserShop;
import com.cake.liulanxiangzhu.pojo.dto.UserShopDTO;
import com.cake.liulanxiangzhu.pojo.dto.UserShopUpdateDTO;
import com.cake.liulanxiangzhu.pojo.vo.UserShopVO;
import com.cake.liulanxiangzhu.mapper.UserShopMapper;
import com.cake.liulanxiangzhu.service.UserShopService;
import com.cake.liulanxiangzhu.utils.LoginUtil;
import com.cake.liulanxiangzhu.web.JsonPage;
import com.cake.liulanxiangzhu.web.ProductCode;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;


/**
 * 用户信息表(UserShop)表服务实现类
 *
 * @author dinghao
 * @since 2023-02-21 21:02:27
 */
@Service
public class UserShopServiceImpl implements UserShopService {
    @Autowired
    private UserShopMapper userShopMapper;

    @Autowired
    private LoginUtil loginUtil;
    /**
     * 通过ID查询单条数据
     *
     *
     * @param request@return 实例对象
     */
    @Override
    public UserShopVO queryById(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userId = String.valueOf(loginUtil.parser(token).get("id", Integer.class));
        UserShopVO userShopVO = userShopMapper.queryDiscont(Integer.parseInt(userId));
        Double discount = userShopVO.getDiscount();
        Double discountN = discount * 10 ;
        userShopVO.setDiscountNm(String.valueOf(discountN)+"折");
        return userShopVO;
    }

    /**
     * 分页查询
     *
     * @param userShop 筛选条件
     * @return 查询结果
     */
    @Override
    public JsonPage<UserShopVO> queryByPage(UserShop userShop, Integer page , Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        return null;
    }

    /**
     * 新增数据
     *
     * @param request
     * @param userShopDto 实例对象
     * @return 实例对象
     */
    @Override
    public void insert(HttpServletRequest request, UserShopDTO userShopDto) {
        String token = request.getHeader("Token");
        String userId = loginUtil.parser(token).getId();
        UserShop userShop = new UserShop();
        BeanUtils.copyProperties(userShopDto,userShop);
        userShop.setUserId(Integer.parseInt(userId));
        userShop.setIsRegister(ProductCode.YES.getCode());
        userShop.setIsVip(ProductCode.YES.getCode());
        userShop.setVipLevel(1);
        userShop.setUpdateTime(new Date());
        userShopMapper.update(userShop);

    }

    /**
     * 修改数据
     *
     * @param request
     * @param userShopUpdateDto 实例对象
     * @return 实例对象
     */
    @Override
    public void update(HttpServletRequest request, UserShopUpdateDTO userShopUpdateDto) {
        String token = request.getHeader("Token");
        String userId = loginUtil.parser(token).getId();
        UserShopVO userShopVO = userShopMapper.queryById(Integer.parseInt(userId));
        Integer isRegister = userShopVO.getIsRegister();
        Integer isVip = userShopVO.getIsVip();
        UserShop userShop = new UserShop();
        BeanUtils.copyProperties(userShopUpdateDto,userShop);
        userShop.setUserId(Integer.parseInt(userId));
        if (isRegister==0){
            userShop.setIsRegister(ProductCode.YES.getCode());
        }
        if (isVip==null || isVip == 0){
            userShop.setIsVip(ProductCode.YES.getCode());
            userShop.setVipLevel(1);
        }
        userShop.setUpdateTime(new Date());
        userShopMapper.update(userShop);

    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public void deleteById(Integer userId) {

    }

    @Override
    public void recharge(HttpServletRequest request, UserShopUpdateDTO userShopUpdateDTO) {
        String token = request.getHeader("Token");
        String userId = loginUtil.parser(token).getId();
        UserShop userShop = new UserShop();
        UserShopVO userShopVO = userShopMapper.queryById(Integer.parseInt(userId));
        System.out.println(userShopVO);
        BigDecimal oldBalance = userShopVO.getBalance();
        BigDecimal payBalance = userShopUpdateDTO.getBalance();
        BigDecimal newBalance = oldBalance.add(payBalance);
        System.out.println(newBalance);
        userShop.setUserId(Integer.parseInt(userId));
        userShop.setBalance(newBalance);
        userShop.setUpdateTime(new Date());
        userShopMapper.update(userShop);

    }


}
